//
//  MyAttentionVCV.m
//  APP_login
//
//  Created by edz on 7/19/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "MyAttentionVC.h"
#import "AttentionUserVC.h"
#import "MyAttentionTitleCell.h"
#import "AttentionTopicVC.h"
#import "Masonry.h"


@interface MyAttentionVC ()
<UIPageViewControllerDelegate,
UIPageViewControllerDataSource,
UICollectionViewDelegate,
UICollectionViewDataSource,
UIScrollViewDelegate>

@property (nonatomic, strong) UIPageViewController *pageViewController;
@property (nonatomic, strong) NSArray *titleArray;//标题数组
@property (nonatomic, strong) NSArray *controllerArray;//控制器数组
@property (nonatomic, strong) UICollectionView *titleConllectionView;//标题collectionView
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *progressUserView;
@property (nonatomic, strong) UIView *progressTopicView;
@property (nonatomic, assign) BOOL isUserPage;
@end

@implementation MyAttentionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的关注";
    self.view.backgroundColor = [UIColor whiteColor];
    _titleArray = @[@"用户",@"话题"];

    AttentionUserVC *userVc = [[AttentionUserVC alloc] init];
    AttentionTopicVC *topicVC = [[AttentionTopicVC alloc] init];
    _controllerArray = @[userVc, topicVC];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"triangle"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UICollectionViewFlowLayout *lay = [[UICollectionViewFlowLayout alloc] init];
    lay.itemSize = CGSizeMake(180, 45);
    lay.minimumLineSpacing = 0;
    lay.minimumInteritemSpacing = 0;
    lay.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _titleConllectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:lay];
    _titleConllectionView.showsHorizontalScrollIndicator = NO;
    _titleConllectionView.backgroundColor = [UIColor whiteColor];
    _titleConllectionView.delegate = self;
    _titleConllectionView.dataSource = self;
    [_titleConllectionView registerClass:[MyAttentionTitleCell class] forCellWithReuseIdentifier:@"titlelable"];
    [self.view addSubview:_titleConllectionView];
    [_titleConllectionView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(45);
    }];
    
    _progressUserView = [[UIView alloc] init];
    [self.view addSubview:_progressUserView];
    [_progressUserView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(32);
        make.left.mas_equalTo(107);
        make.size.mas_equalTo(CGSizeMake(20, 2));
    }];
    _progressUserView.backgroundColor = [UIColor blackColor];
    
    //参数一 滚动试图间距 二 书脊的位置
    NSDictionary *options = @{UIPageViewControllerOptionInterPageSpacingKey:@(20)};
    //初始化参数 样式滚动还是书 水平，垂直 参数
    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
    //设置代理
    _pageViewController.delegate = self;
    _pageViewController.dataSource = self;
    NSArray *arry = [NSArray arrayWithObject:_controllerArray[0]];
    _currentIndex = 0;
    _isUserPage = YES;
    [_pageViewController setViewControllers:arry direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [_pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(42);
        make.bottom.mas_equalTo(0);
    }];
    [_titleConllectionView bringSubviewToFront:_pageViewController.view];
    _scrollView = [self findScrollView];
    _scrollView.delegate = self;
}
#pragma mark -ConllectionView协议
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _controllerArray.count;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyAttentionTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"titlelable" forIndexPath:indexPath];
    cell.titleLable.text = _titleArray[indexPath.row];
    if (_currentIndex == indexPath.row) {
        cell.titleLable.textColor = [UIColor blackColor];
    }else{
        cell.titleLable.textColor = [UIColor grayColor];
    }
    return cell;
}
//点击事件
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *vc = [_controllerArray objectAtIndex:indexPath.row];
    [self judgecurrentIndex];
    if (indexPath.row > _currentIndex) {
        [_pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    }else{
        [_pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    }
    _currentIndex = indexPath.row;
    [self.titleConllectionView reloadData];
}

#pragma mark - PageView协议
//展示上一页
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSInteger index = [self.controllerArray indexOfObject:viewController];
    if (index == 0 || index == NSNotFound) {
        return nil;
    }
    index --;
    return [self.controllerArray objectAtIndex:index];
}
//展示下一页
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSInteger index = [self.controllerArray indexOfObject:viewController];
    if (index == self.controllerArray.count -1 || index == NSNotFound) {
       return nil;
   }
   index ++;
   return [self.controllerArray objectAtIndex:index];
}

//滑动将要切换时
- (void) pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers{
    UIViewController *vc = [pendingViewControllers firstObject];
    _currentIndex = [_controllerArray indexOfObject:vc];
}

//滑动结束后
- (void) pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    if (completed) {
        [_titleConllectionView reloadData];
        [self judgecurrentIndex];
    }
}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x > 434 && _isUserPage) {
        CGFloat x = (scrollView.contentOffset.x - 434.0);
        if (x <= 170) {
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(20+x);
            }];
        }
        if (170 < x & x <= 183){
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(190);
                make.left.mas_equalTo(107+(x-170));
            }];
        }
        if (184 < x & x < 354) {
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(190-(x-184));
                make.left.mas_equalTo(107+(x-170));
            }];
        }
        if (x > 354){
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(20);
                make.left.mas_equalTo(292);
            }];
        }
    }
    if (scrollView.contentOffset.x < 434 && 0 < scrollView.contentOffset.x && !_isUserPage) {
        CGFloat x = (434.0-scrollView.contentOffset.x);
        if (x <= 170) {
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(20+x);
                make.left.mas_equalTo(292-x);
            }];
        }
        if (170 < x && x< 185) {
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(190);
                make.left.mas_equalTo(292-x);
            }];
        }
        if (186 < x && x < 357) {
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(190-(x-185));
                make.left.mas_equalTo(107);
            }];
        }
        if (x > 357) {
            [self.progressUserView mas_updateConstraints:^(MASConstraintMaker *make){
                make.width.mas_equalTo(20);
                make.left.mas_equalTo(107);
            }];
            _progressUserView.frame = CGRectMake(107, 32, 20, 2);
        }
    }
    
}
-(void)judgecurrentIndex{
    if (_currentIndex == 0) {
           _isUserPage = YES;
       }else{
           _isUserPage = NO;
       }
}

- (UIScrollView*)findScrollView{
    UIScrollView *scrollView;
    for (id subview in _pageViewController.view.subviews) {
        if ([subview isKindOfClass:UIScrollView.class]) {
            scrollView = subview;
            break;
        }
    }
    return scrollView;
}

- (void) leftButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
